<?php
namespace app\Backend\controller;;
use think\Db;
use clt\Leftnav;
use app\Backend\model\User as UserModel;
use think\Validate;
class User extends Common
{
    public function _initialize(){
        parent::_initialize();
    }
    public function index()
    {
        return $this->fetch();
    }

    //员工列表
    public function userList(){
        // $result=base64_encode(create_qrcode());
        // $a = "<img src='data:image/png;base64,{$result}'  />";
        // return $a;

// -------------------------------
        $list = db('user')->order('id desc')->select();
        $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list,'rel'=>1];
        $this->assign('lists',$result['data']);
        return view();
    }

    //删除用户
    public function userDel(){
        $id             = input('id');
        $map['id']      = $id;
        $map['is_sys']  = 0; //0=>不是系统管理员，可删除!
        return $result = ['code'=>0,'msg'=>'用户信息删除失败!','url'=>''];
        // if (db('user')->where($map)->delete()){
        //     return $result = ['code'=>1,'msg'=>'删除成功!','url'=>'/reload'];
        // }else{
        //     return $result = ['code'=>0,'msg'=>'用户信息删除失败!','url'=>''];
        // }
    }

    //修改用户状态
    public function userState(){
        $id=input('post.id');
        if (empty($id)){
            $result['status'] = 0;
            $result['info'] = '用户ID不存在!';
            $result['url'] = url('adminList');
            exit;
        }
        $status=db('admin')->where('admin_id='.$id)->value('is_open');//判断当前状态情况
        if($status==1){
            $data['is_open'] = 0;
            db('admin')->where('admin_id='.$id)->update($data);
            $result['status'] = 1;
            $result['open'] = 0;
        }else{
            $data['is_open'] = 1;
            db('admin')->where('admin_id='.$id)->update($data);
            $result['status'] = 1;
            $result['open'] = 1;
        }
        return $result;
    }

    //更新管理员信息
    public function userEdit(){
        if(request()->isPost()){
            $data   = input('post.');
            $pwd    = input('post.pwd');

            $map['id']    = array('neq',input('post.id'));
            $where['admin_id']  = input('post.admin_id');
            if(input('post.username')){
                $map['username'] = input('post.username');
                $check_user = Usermodel::get($map);
                if ($check_user) {
                    return $result = ['code'=>0,'msg'=>'用户已存在，请重新输入用户名!'];
                }
            }
            if ($pwd){
                $data['pwd']=input('post.pwd','','md5');
            }else{
                unset($data['pwd']);
            }
            $msg = $this->validate($data,'User');
            if($msg!='true'){
                return $result = ['code'=>0,'msg'=>$msg];
            }
            if(!empty($data['email']) && !is_email($data['email'])){
                return $result = ['code'=>0,'msg'=>'输入邮箱格式不对'];
            }
            Usermodel::update($data);
            return $result = ['code'=>1,'msg'=>'用户修改成功!','url'=>'/reload'];
        }else{
            $info = Usermodel::get(['id'=>input('id')]);
            $this->assign('row', $info);
            $this->assign('title',lang('edit').lang('admin'));
            return view('userForm');
        }
    }


}